package com.zjw.zy.coreui.fragment.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.zjw.zy.R
import com.zjw.zy.base.BaseRecyclerAdapter
import com.zjw.zy.bean.ImageInfo
import com.zjw.zy.coreui.fragment.holder.ImageContentHolder

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/10/27 9:36
 * @description：
 */


class ImageContentAdapter(
    var usingPath: String = "",
    listener: ItemListener<ImageContentHolder>
) :
    BaseRecyclerAdapter<ImageInfo, ImageContentHolder>(listener) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageContentHolder {
        val item = LayoutInflater.from(parent.context)
            .inflate(R.layout.holder_image_content, parent, false)

        return ImageContentHolder(item)
    }


    fun notifyImageInsert(image: ImageInfo) {
        val position = data.size
        this.data.add(image)
        notifyItemInserted(position)
    }

    override fun onBindViewHolder(holder: ImageContentHolder, position: Int) {
        super.onBindViewHolder(holder, position)

        holder.markUsing(usingPath)
    }

    fun notifyMarkUsing(usingPath: String) {
        var oldPosition = -1
        var newPosition = -1
        for (i in data.indices) {
            if (data[i].path == this.usingPath) {
                oldPosition = i
            }

            if (data[i].path == usingPath) {
                newPosition = i
            }

            if (oldPosition != -1 && newPosition != -1) {
                break
            }
        }

        this.usingPath = usingPath

        notifyItemChanged(oldPosition)
        notifyItemChanged(newPosition)
    }
}